import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:im_app/demo/MainDemo.dart';
import 'package:super_sliver_list/super_sliver_list.dart';

import '../../im/page/bloc/DraggableBloc.dart';
import '../../im/page/view/DraggableFloatingView.dart';
import '../../im/util/DialogUtil.dart';

/// Example for EmojiPickerFlutter
class SupListPage extends StatefulWidget {
  @override
  _SupListState createState() => _SupListState();
}

class _SupListState extends State<SupListPage> {
  final _listController = ListController();
  final _scrollController = ScrollController();

  var jumpIndex = 0;

  @override
  void initState() {
    super.initState();

    WidgetsBinding.instance.addPostFrameCallback((_){
      jumpToItem(999);
    });
  }

  @override
  void dispose() {
    super.dispose();
    _scrollController.dispose();
    _listController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: buildAppbar(),
        body: Column(
          children: [
            Expanded(child: SuperListView.builder(
              listController: _listController,
              controller: _scrollController,
              itemCount: 1000,
              itemBuilder: (context, index) {
                return ListTile(title: Text('Item $index'));
              },
            )),
            Row(children: [
              GestureDetector(
                onTap: () {
                  final random = Random();
                  var size =random.nextInt(1000);
                  setState(() {
                    this.jumpIndex = size;
                  });

                  jumpToItem(size);
                },
                child: Text('jump: $jumpIndex'),
              ),
              TextButton(onPressed: ()=>overlay(), child: Text('overlay')),
            ],),
          ],
        ));
  }

  void jumpToItem(int index) {
    _listController.jumpToItem(
      index: index,
      scrollController: _scrollController,
      alignment: 0.5,
    );
  }

  void animateToItem(int index) {
    _listController.animateToItem(
      index: index,
      scrollController: _scrollController,
      alignment: 0.5,
      // You can provide duration and curve depending on the estimated
      // distance between currentPosition and the target item position.
      duration: (estimatedDistance) => Duration(milliseconds: 250),
      curve: (estimatedDistance) => Curves.easeInOut,
    );
  }

  void overlay() {
    var child = BlocProvider(create: (_)=> DraggableCubit(), child: DraggableFloatingCubitView(child: Material(
      elevation: 4.0,
      borderRadius: BorderRadius.all(Radius.circular(10)),
      child: Container(
        width: 200,
        height: 100,
        padding: EdgeInsets.all(16.0),
        color: Colors.white,
        child: Text('11111111111111111111'),
      ),
    ),),);
    showOverlay(context, child);
  }
}
